package com.springstudy.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import javax.annotation.PostConstruct;

@SpringBootApplication(scanBasePackages = "com.springstudy.redisListener")
@ComponentScan(basePackages = "com.springstudy.redisListener")
public class RedisApplication {

    @Autowired
    private RedisTemplate redisTemplate = null;

    // 使用注解@PostConstruct定义自定义后初始化方法，该方法会在@Autowired注解的对象注入之后执行
    @PostConstruct
    public void init() {
        initRedisTemplate();
    }

    //设置RedisTemplate的序列化器
    private void initRedisTemplate() {
        RedisSerializer stringSerializer = redisTemplate.getStringSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        //redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
    }

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }

}
